﻿@inject IBlogService Service
@inject IClientUriGenerator UrlGenerator
@inject MessageService MessageService 

<Loader OnLoad="@OnLoad" AutoLoad="@true" @ref="loader" UseSkeleton>
    <ListItem @key="@Id" Style="display: block">
        <ListItemMeta Avatar="@("")">
            <TitleTemplate>
                <h4>
                    @if (ShowType)
                    {
                        switch (Post.Type)
                        {
                            case PostType.Article:
                                <MdiIcon Type="@IconNames.Articles" />
                                break;
                            case PostType.Slides:
                                <MdiIcon Type="@IconNames.Slides" />
                                break;
                            case PostType.Note:
                                <MdiIcon Type="@IconNames.Notes" />
                                break;
                            default:
                                break;
                        }
                    }
                    <a href="@UrlGenerator.Post(Post)" style="color:black">
                        <span>@Post.Title</span>
                    </a>
                </h4>
                <div>
                    <PostMetadataDisplay Value="@Post" />
                </div>
            </TitleTemplate>
        </ListItemMeta>

        @if (!IsProtected)
        {
            <p>
                <DocumentPreviewDisplay Document="@Post.Content" />
            </p>
        }
        else
        {
            <p>Protected Post</p>
        }
        <AuthorizeView>
            <Authorized>
                <ButtonLink Href="@($"posts/{Uri.EscapeDataString(Post.Id)}/edit")">
                    <MdiIcon Type="@IconNames.Edit"></MdiIcon>
                    <span>Edit</span>
                </ButtonLink>
                <Popconfirm Title="@($"Are you sure {(IsDeleted ? "revert" : "delete")} this post?")" OnConfirm="OnDelete" Placement="@PlacementType.BottomRight">
                    <Button Danger="@(!IsDeleted)">
                        <MdiIcon Type="@(IsDeleted ? IconNames.Revert : IconNames.Delete)"></MdiIcon>
                        <span>@(IsDeleted ? "Revert" : "Delete")</span>
                    </Button>
                </Popconfirm>
            </Authorized>
        </AuthorizeView>
    </ListItem>
</Loader>

@code {
    [Parameter]
    public bool ShowType { get; set; } = false;

    [Parameter]
    public string Id { get; set; }

    private Post Post { get; set; }

    private bool IsProtected { get; set; } = false;

    private bool IsDeleted { get; set; }

    private Loader loader;

    protected override async Task OnParametersSetAsync()
    {
        if (loader is not null)
        {
            await loader.Load();
        }
    }

    private async Task OnLoad()
    {
        Post = null;
        IsProtected = false;

        Post = await Service.PostService.Get(Id);
        IsProtected = await Service.PostService.Protector.IsProtected(Post.Content);
    }

    private async Task OnDelete()
    {
        if (IsDeleted)
        {
            var pass = await Service.PostService.Create(Post);
            if (!string.IsNullOrWhiteSpace(pass))
            {
                IsDeleted = false;
                _ = MessageService.Success("Reverted post successfully.");
            }
            else
            {
                _ = MessageService.Error("Failed to revert post.");
            }
        }
        else
        {
            var pass = await Service.PostService.Delete(Post.Id);
            if (pass)
            {
                IsDeleted = true;
                _ = MessageService.Success("Deleted post successfully.");
            }
            else
            {
                _ = MessageService.Error("Failed to delete post.");
            }
        }
    }
}
